﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel;

namespace PDX.Validation
{
    /// <summary>
    /// Looks for validation failures defined by System.ComponentModel.IDataErrorInfo
    /// on the <see cref="IPropertyResolution"/>.
    /// </summary>
    public class DataErrorInfoAdapter : IValidationAdapter
    {
        /// <summary>
        /// Gets a validation failure message.
        /// Return null or String.Empty to indicate there is no error.
        /// </summary>
        /// <param name="context">The context.</param>
        /// <returns></returns>
        public string GetMessage(Context context)
        {
            var resolution = context.GetResolution();

            if (resolution == null || resolution.Property == null)
            {
                return string.Empty;
            }

            var iData = resolution.ResolvedDC as IDataErrorInfo;
            if (iData != null)
            {
                return iData[resolution.Property.Name];
            }

            return string.Empty;
        }
    }
}
